package ua.vn.ticket_scanner.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import ua.vn.ticket_scanner.TApplication;
import ua.vn.ticket_scanner.user.UserData;

/**
 * Created by Ivan on 21.06.13.
 */
public class TPreferenceManager {

    private static final String HTTP_TIMEOUT = "http_timeout";
    private static final String MAIN_SERVER_URL = "main_server_url";
    private static final String TRANSPORT_SERVER_URL = "transport_server_url";

    private static final int HTTP_TIMEOUT_DEFAULT = 5000;
    private static final String MAIN_SERVER_URL_DEFAULT = "http://77.121.97.167:8888/MainServer/cmd";
    private static final String TRANSPORT_SERVER_URL_DEFAULT = "http://77.121.97.167:8889/cmd";

    private static final String CONTROLLER_ID = "controller_id";
    private static final String CONTROLLER_TOKEN = "controller_token";
    private static final String CONTROLLER_FIRST_NAME = "controller_first_name";
    private static final String CONTROLLER_SECOND_NAME = "controller_second_name";

    private static TPreferenceManager instance = null;

    private TPreferenceManager() {
        load();
        initSettings();
    }

    public static TPreferenceManager getInstance() {
        if (instance == null)
            instance = new TPreferenceManager();
        return instance;
    }

    private SharedPreferences.Editor getEditor() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        return prefs.edit();
    }

    private void load() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        String id = prefs.getString(CONTROLLER_ID, null);
        String token = prefs.getString(CONTROLLER_TOKEN, null);
        String firstName = prefs.getString(CONTROLLER_FIRST_NAME, null);
        String secondName = prefs.getString(CONTROLLER_SECOND_NAME, null);
        UserData.getInstance().setId(id);
        UserData.getInstance().setToken(token);
        UserData.getInstance().setFirstName(firstName);
        UserData.getInstance().setSecondName(secondName);
    }

    private void initSettings() {
        SharedPreferences.Editor editor = getEditor();
        if (getStringHttpTimeout() == null)
            editor.putString(HTTP_TIMEOUT, String.valueOf(HTTP_TIMEOUT_DEFAULT));
        if (getMainServerUrl() == null)
            editor.putString(MAIN_SERVER_URL, MAIN_SERVER_URL_DEFAULT);
        if (getTransportServerUrl() == null)
            editor.putString(TRANSPORT_SERVER_URL, TRANSPORT_SERVER_URL_DEFAULT);
        editor.commit();
    }

    public void restoreDefaultParams() {
        setHttpTimeout(HTTP_TIMEOUT_DEFAULT);
        setMainServerUrl(MAIN_SERVER_URL_DEFAULT);
        setTransportServerUrl(TRANSPORT_SERVER_URL_DEFAULT);
    }

    public void saveUserData() {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(CONTROLLER_ID, UserData.getInstance().getId());
        editor.putString(CONTROLLER_TOKEN, UserData.getInstance().getToken());
        editor.putString(CONTROLLER_FIRST_NAME, UserData.getInstance().getFirstName());
        editor.putString(CONTROLLER_SECOND_NAME, UserData.getInstance().getSecondName());
        editor.commit();
    }

    private String getStringHttpTimeout() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        return prefs.getString(HTTP_TIMEOUT, "0");
    }

    public void setHttpTimeout(int timeout) {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(HTTP_TIMEOUT, String.valueOf(timeout));
        editor.commit();
    }

    public int getHttpTimeout() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        return Integer.valueOf(getStringHttpTimeout());
    }

    public String getMainServerUrl() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        return prefs.getString(MAIN_SERVER_URL, null);
    }

    private void setMainServerUrl(String serverUrl) {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(MAIN_SERVER_URL, serverUrl);
        editor.commit();
    }

    public String getTransportServerUrl() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(TApplication.getInstance());
        return prefs.getString(TRANSPORT_SERVER_URL, null);
    }

    private void setTransportServerUrl(String serverUrl) {
        SharedPreferences.Editor editor = getEditor();
        editor.putString(TRANSPORT_SERVER_URL, serverUrl);
        editor.commit();
    }
}
